﻿using Batman.Maintenance.Enums;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Batman.Maintenance.DTO
{
    public class MaintenanceMainTable<T> where T : class
    {
        public MaintenanceMainTable()
        {
            Columns = new List<MaintenaceColumn>();

            _includeList = new List<Expression<Func<T, object>>>();
        }

        /// <summary>
        /// 是否有全选列
        /// </summary>
        public bool HasSelection { get; set; } = true;

        /// <summary>
        /// 请求数据接口地址
        /// </summary>
        public string ReadAction { get; set; }

        /// <summary>
        /// 列表
        /// </summary>
        public List<MaintenaceColumn> Columns { get; set; }

        /// <summary>
        /// 是否有多选框列
        /// </summary>
        public bool HasSelectionColumn { get; set; }

        #region 连带查询

        /// <summary>
        /// 连带查询
        /// </summary>
        private List<Expression<Func<T, object>>> _includeList { get; set; }

        /// <summary>
        /// 获取连带查询设置
        /// </summary>
        /// <returns></returns>
        public List<Expression<Func<T, object>>> GetIncludeList()
        {
            return _includeList;
        }

        /// <summary>
        /// 添加连带查询设置
        /// </summary>
        /// <param name="include"></param>
        public MaintenanceMainTable<T> Include(Expression<Func<T, object>> include)
        {
            _includeList.Add(include);

            return this;
        }

        #endregion


        #region 默认排序

        /// <summary>
        /// 排序
        /// </summary>
        private KeyValuePair<Expression<Func<T, object>>, OrderBy> _sort { get; set; }

        /// <summary>
        /// 设置默认排序
        /// </summary>
        /// <param name="expression"></param>
        public MaintenanceMainTable<T> Sort(Expression<Func<T, object>> expression, OrderBy orderBy)
        {
            this._sort = new KeyValuePair<Expression<Func<T, object>>, OrderBy>(expression, orderBy);

            return this;
        }

        /// <summary>
        /// 获取默认排序设置
        /// </summary>
        /// <returns></returns>
        public KeyValuePair<Expression<Func<T, object>>, OrderBy> GetDefaultSort()
        {
            return _sort;
        }

        /// <summary>
        /// 然后排序
        /// </summary>
        private KeyValuePair<Expression<Func<T, object>>, OrderBy> _sortThenBy { get; set; }

        /// <summary>
        /// 设置然后排序
        /// </summary>
        /// <param name="expression"></param>
        public MaintenanceMainTable<T> ThenBy(Expression<Func<T, object>> expression, OrderBy orderBy)
        {
            this._sortThenBy = new KeyValuePair<Expression<Func<T, object>>, OrderBy>(expression, orderBy);

            return this;
        }

        /// <summary>
        /// 获取然后排序设置
        /// </summary>
        /// <returns></returns>
        public KeyValuePair<Expression<Func<T, object>>, OrderBy> GetDefaultThenBy()
        {
            return _sortThenBy;
        }
        #endregion
    }
}
